package com.aqgj.Attachment.dao;

import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.util.BaseDaoImpl;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.io.InputStream;


@Repository("AttachmentDao")
public class AttachmentDao extends BaseDaoImpl<Attachment> {

    /**
     * 保存文件.
     * @param attachment 实体文件
     * @param inputStream 输入流
     */
    public boolean saveAttachToDB(Attachment attachment, InputStream inputStream){
        boolean success=false;
        try {
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            attachment.setFileBody(data);
            this.save(attachment);
            success=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

    public void update(Attachment attachment) {
        this.getHibernateTemplate().merge(attachment);
    }
}